package gameengine.com;

import resolution.DEF;
import gameengine.com.state.StateGameplay;
import gameengine.com.state.StateMainMenu;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint.Style;
import android.graphics.RectF;

public class Dialog
{
	public static final int DIALOG_TYPE_CONFIRM = 0;
	public static final int DIALOG_TYPE_NOTICE = 1;
	//public static int DIALOG_TYPE_CONFIRM = -1;

	//
	public static final int DIALOG_NEXTSTATE_EXIT = 0;
	public static final int DIALOG_BACK_TO_MAINMENU_EXIT = 1;
	public static final int DIALOG_RESTART_GAME = 2;
	//
	public static int dialogType = -1;
	public static int dialogNextState = -1;
	public static String dialogText = null;

	//
	public static int DIALOG_CONFIRM_X = MainActivity.SCREEN_WIDTH / 6;
	public static int DIALOG_CONFIRM_Y = MainActivity.SCREEN_HEIGHT / 6;
	public static int DIALOG_CONFIRM_W = MainActivity.SCREEN_WIDTH - 2 * MainActivity.SCREEN_WIDTH / 6;
	public static int DIALOG_CONFIRM_H = MainActivity.SCREEN_HEIGHT - 2 * MainActivity.SCREEN_HEIGHT / 6;
	public static int DIALOG_BUTTON_CONFIRM_W = 93;
	public static int DIALOG_BUTTON_CONFIRM_H = 93;
	public static RectF rectF = null;
	public static boolean isShowDialog = false;

	public static void showDialog(int type, String textStringLabel, String textString, int nextState)
	{
		isShowDialog = true;
		dialogType = type;
		dialogText = textString;
		dialogNextState = nextState;
		rectF = new RectF(MainActivity.SCREEN_WIDTH / 6, MainActivity.SCREEN_HEIGHT / 6, MainActivity.SCREEN_WIDTH / 6 + DIALOG_CONFIRM_W, MainActivity.SCREEN_HEIGHT / 6 + DIALOG_CONFIRM_H);
	}

	public static void hideDialog()
	{
		isShowDialog = false;
	}

	public static void drawDialog(Canvas c)
	{
		MainActivity.mainPaint.setStyle(Style.FILL);
		MainActivity.mainPaint.setARGB(200, 0, 0, 0);
		c.drawRect(0, 0, MainActivity.SCREEN_WIDTH, MainActivity.SCREEN_HEIGHT, MainActivity.mainPaint);
		MainActivity.mainPaint.setStyle(Style.FILL_AND_STROKE);
		MainActivity.mainPaint.setColor(Color.rgb(67, 120, 167));
		c.drawRoundRect(rectF, 25, 25, MainActivity.mainPaint);
		//draw text
		MainActivity.fontWhite.drawString(c, dialogText, MainActivity.SCREEN_WIDTH / 2, MainActivity.SCREEN_HEIGHT / 2 - 180, DIALOG_CONFIRM_W - 100, BitmapFont.ALIGN_CENTER);
		int y = MainActivity.SCREEN_HEIGHT / 6 + DIALOG_CONFIRM_H - 40 - 20;
		int x1 = DIALOG_CONFIRM_X + 90 + DIALOG_BUTTON_CONFIRM_W / 2;
		int x2 = DIALOG_CONFIRM_X + DIALOG_CONFIRM_W - 90 - DIALOG_BUTTON_CONFIRM_W / 2;
		//draw button
		switch (dialogType)
		{
		case DIALOG_TYPE_CONFIRM:

			if (MainActivity.isTouchDrapInRect(x1 - DIALOG_BUTTON_CONFIRM_H / 2, y - DIALOG_BUTTON_CONFIRM_H / 2, DIALOG_BUTTON_CONFIRM_W, DIALOG_BUTTON_CONFIRM_H))
				StateGameplay.spriteDPad.drawAFrame(MainActivity.mainCanvas, DEF.FRAME_CANCEL_HIGHTLIGHT, x1, y);
			else
				StateGameplay.spriteDPad.drawAFrame(MainActivity.mainCanvas, DEF.FRAME_CANCEL_NORMAL, x1, y);

			if (MainActivity.isTouchDrapInRect(x2 - DIALOG_BUTTON_CONFIRM_H / 2, y - DIALOG_BUTTON_CONFIRM_H / 2, DIALOG_BUTTON_CONFIRM_W, DIALOG_BUTTON_CONFIRM_H))
				StateGameplay.spriteDPad.drawAFrame(MainActivity.mainCanvas, DEF.FRAME_OK_HIGHTLIGHT, x2, y);
			else
				StateGameplay.spriteDPad.drawAFrame(MainActivity.mainCanvas, DEF.FRAME_OK_NORMAL, x2, y);
			break;
		case DIALOG_TYPE_NOTICE:
			if (MainActivity.isTouchDrapInRect(x2 - DIALOG_BUTTON_CONFIRM_H / 2, y - DIALOG_BUTTON_CONFIRM_H / 2, DIALOG_BUTTON_CONFIRM_W, DIALOG_BUTTON_CONFIRM_H))
				StateGameplay.spriteDPad.drawAFrame(MainActivity.mainCanvas, DEF.FRAME_OK_HIGHTLIGHT, x2, y);
			else
				StateGameplay.spriteDPad.drawAFrame(MainActivity.mainCanvas, DEF.FRAME_OK_NORMAL, x2, y);
			break;
		default:
			break;
		}

		//		fontsmall_White.drawString(mainCanvas, arrayMenuString[i], MENU_POSITION_X, y - 20, BitmapFont.ALIGN_CENTER);		

	}

	public static void updateDialog()
	{
		int y = MainActivity.SCREEN_HEIGHT / 6 + DIALOG_CONFIRM_H - 40 - 20;
		int x1 = DIALOG_CONFIRM_X + 90 + DIALOG_BUTTON_CONFIRM_W / 2;
		int x2 = DIALOG_CONFIRM_X + DIALOG_CONFIRM_W - 90 - DIALOG_BUTTON_CONFIRM_W / 2;
		switch (dialogType)
		{
		case DIALOG_TYPE_CONFIRM:

			if (MainActivity.isTouchReleaseInRect(x1 - DIALOG_BUTTON_CONFIRM_H / 2, y - DIALOG_BUTTON_CONFIRM_H / 2, DIALOG_BUTTON_CONFIRM_W, DIALOG_BUTTON_CONFIRM_H))//cancel
			{
				hideDialog();
			} else if (MainActivity.isTouchReleaseInRect(x2 - DIALOG_BUTTON_CONFIRM_H / 2, y - DIALOG_BUTTON_CONFIRM_H / 2, DIALOG_BUTTON_CONFIRM_W, DIALOG_BUTTON_CONFIRM_H))//ok
			{
				Dialog.hideDialog();
				switch (dialogNextState)
				{
				case DIALOG_NEXTSTATE_EXIT:

					MainActivity.mainActivity.finish();
					break;
				case DIALOG_BACK_TO_MAINMENU_EXIT:
					MainActivity.changeState(MainActivity.STATE_MAINMENU, true, true);
					break;
				case DIALOG_RESTART_GAME:
					MainActivity.changeState(MainActivity.STATE_GAMEPLAY, true, true);
				break;
				default:
					break;
				}

			}
			break;
		case DIALOG_TYPE_NOTICE:
			if (MainActivity.isTouchReleaseInRect(x2 - DIALOG_BUTTON_CONFIRM_H / 2, y - DIALOG_BUTTON_CONFIRM_H / 2, DIALOG_BUTTON_CONFIRM_W, DIALOG_BUTTON_CONFIRM_H))//ok
			{
				switch (dialogNextState)
				{
				//case DIALOG_NEXTSTATE_EXIT:
				//	MainActivity.mainActivity.finish();
				//	break;
				//case DIALOG_BACK_TO_MAINMENU_EXIT:
				//	break;

				default:
					break;
				}
			}
			break;
		default:
			break;
		}

	}
}
